package com.gxa.insurance.controller;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.pojo.Refund;
import com.gxa.insurance.pojo.RefundType;
import com.gxa.insurance.service.RefundService;
import com.gxa.insurance.util.JsonResult;
import com.gxa.insurance.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Min
 */
@RestController
@RequestMapping("/refund")
@CrossOrigin
public class RefundController {

    @Autowired
    private RefundService service;

    @ApiOperation("退款页面分页查询的接口")
    @GetMapping("/selectAllByPage")
    public Map SelectAllByPage(@RequestParam("page")Integer page,@RequestParam("pageSize")Integer pageSize) throws BusinessException {
        if(page==null || pageSize==null){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        Map<String,Object> refundList=service.selectRefundByPage(page,pageSize);
        JsonResult jsonResult = new JsonResult();
        if(refundList.size()<=0){
            jsonResult.setCode("0");
            jsonResult.setMsg("查询失败");
        }else{
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(refundList.get("list"));
            jsonResult.put("count",refundList.get("count"));
        }
        return jsonResult.getMap();
    }

    @ApiOperation("根据订单类型查询")
    @GetMapping("/selectByStatus")
    public Map selectByType(@RequestParam("status")String status,@RequestParam("page")Integer page,@RequestParam("pageSize")Integer pageSize) throws BusinessException {
        if(StringUtil.isEmpty(status)){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        Map<String,Object> refundList=service.selectByStatus(status,page,pageSize);
        JsonResult jsonResult = new JsonResult();
        if(refundList.size()<=0){
            jsonResult.setCode("0");
            jsonResult.setMsg("查询失败");
        }else{
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(refundList.get("list"));
            jsonResult.put("count",refundList.get("count"));
        }
        return jsonResult.getMap();
    }

    @ApiOperation("退款")
    @GetMapping("/returnMoney")
    public Map returnMoney(@RequestParam("returnMoney")Boolean returnMoney,@RequestParam("status")String status,@RequestParam("id")Integer id) throws BusinessException {
        if(returnMoney==null || StringUtil.isEmpty(status)){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        Boolean isOk=service.updateIsReturn(returnMoney,status,id);
        JsonResult jsonResult = new JsonResult();
        if(!isOk){
            jsonResult.setCode("0");
            jsonResult.setMsg("更新失败");
        }else{
            jsonResult.setCode("1");
            jsonResult.setMsg("更新成功");
        }
        return jsonResult.getMap();
    }

    @ApiOperation("驳回，并给出理由")
    @ApiImplicitParam(name = "map",value = "{\"isRefuse\":\"true\",\"reason\":\"不拉不拉不拉\",\"status\":\"已退款\",\"id\":\"1\"}")
    @PostMapping("/refuse")
    public Map refuseReturn(@RequestBody Map<String,String> map) throws BusinessException {
        if(map==null || map.size()<=0){
            throw new BusinessException(ExceptionCode.WRONG_VALUE);
        }
        Boolean isOk=service.isRefuse(map);
        JsonResult jsonResult = new JsonResult();
        if(!isOk){
            jsonResult.setCode("0");
            jsonResult.setMsg("更新失败");
        }else{
            jsonResult.setCode("1");
            jsonResult.setMsg("更新成功");
        }
        return jsonResult.getMap();
    }

    @ApiOperation("获取所有的类型")
    @GetMapping("/getAllStatus")
    public Map getStatus(){
        List<RefundType> refundTypes=service.selectAllStatus();
        JsonResult jsonResult = new JsonResult();
        if(refundTypes.size()<=0 || refundTypes==null){
            jsonResult.setCode("0");
            jsonResult.setMsg("查询失败");
        }else{
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(refundTypes);
        }
        return jsonResult.getMap();
    }
}
